//
//  BDSettingTableViewController.m
//  BeidouMall
//
//  Created by qie on 16/3/31.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDSettingTableViewController.h"
#import "UIScrollView+TwitterCover.h"
#import "BDNavigationController.h"
#import "BDOrderHomeViewController.h"

@interface BDSettingTableViewController () <UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UIButton *userImageButton;
@property (weak, nonatomic) IBOutlet UIButton *userSettingButton;
@property (weak, nonatomic) IBOutlet UIButton *userTitleButton;
@property (weak, nonatomic) IBOutlet UIImageView *userSettingImageView;

@end

@implementation BDSettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView addTwitterCoverWithImage:[UIImage imageNamed:@"setting_top_bg"] withFrame:CGRectMake(0, 0, kScreen_Width, CHTwitterCoverViewHeight)];
    //tableView上面多出来20个像素，是因为自动布局的缘故
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    if ([BDAccountManagerModel shareInstance].isLogin) {
        self.userNameLabel.hidden = NO;
        self.userSettingButton.hidden = NO;
        self.userSettingImageView.hidden = NO;
        self.userTitleButton.hidden = YES;
        self.userNameLabel.text = [BDAccountManagerModel shareInstance].userName;
        [self.userImageButton setBackgroundImage:[UIImage imageNamed:@"userImg"] forState:UIControlStateNormal];
    } else {
        self.userNameLabel.hidden = YES;
        self.userSettingButton.hidden = YES;
        self.userSettingImageView.hidden = YES;
        self.userTitleButton.hidden = NO;
        [self.userImageButton setBackgroundImage:[UIImage imageNamed:@"userImg_no"] forState:UIControlStateNormal];
    }
}

- (void)dealloc
{
    [self.tableView removeTwitterCoverView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0;
}

#pragma mark - IBActions

- (IBAction)userButtonPressed:(UIButton *)sender {
    if ([BDAccountManagerModel shareInstance].isLogin) {
        
    } else {
        [self performSegueWithIdentifier:@"loginSegue" sender:nil];
    }
}

- (IBAction)myAddrButtonPressed:(UIButton *)sender {
    if ([BDAccountManagerModel shareInstance].login) {
        [self performSegueWithIdentifier:@"myAddrSegue" sender:nil];
    } else {
        [self performSegueWithIdentifier:@"loginSegue" sender:nil];
    }
    
}

- (IBAction)userSettingButtonPressed:(UIButton *)sender {
    [self performSegueWithIdentifier:@"userSettingSegue" sender:nil];
}


- (IBAction)myOrderButtonPressed:(UIButton *)sender {
    if ([BDAccountManagerModel shareInstance].login) {
        UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        BDNavigationController *nav_orderVc = [stroyboard instantiateViewControllerWithIdentifier:@"NavOrderStoryboardID"];
        BDOrderHomeViewController *orderVc = (BDOrderHomeViewController *)nav_orderVc.topViewController;
        orderVc.block = ^(NSString *str){
            if ([str isEqualToString:@"success"]) {
                self.tabBarController.selectedIndex = 0;
            }
        };
        [self presentViewController:nav_orderVc animated:YES completion:nil];
    } else {
        [self performSegueWithIdentifier:@"loginSegue" sender:nil];
    }
}

- (IBAction)fourOrderButtonPress:(UIButton *)sender {
    if ([BDAccountManagerModel shareInstance].login) {
        NSInteger index;
        switch (sender.tag) {
            case 201:
                index = 1;
                break;
            case 202:
                index = 2;
                break;
            case 203:
                index = 3;
                break;
            case 204:
                index = 4;
                break;
            default:
                break;
        }
        UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        BDNavigationController *nav_orderVc = [stroyboard instantiateViewControllerWithIdentifier:@"NavOrderStoryboardID"];
        BDOrderHomeViewController *orderVc = (BDOrderHomeViewController *)nav_orderVc.topViewController;
        orderVc.selectIndex = index;
        orderVc.block = ^(NSString *str){
            if ([str isEqualToString:@"success"]) {
                self.tabBarController.selectedIndex = 0;
            }
        };
        [self presentViewController:nav_orderVc animated:YES completion:nil];
    } else {
        [self performSegueWithIdentifier:@"loginSegue" sender:nil];
    }
}



@end
